package com.zt.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.entity.AdminLog;
import com.zt.mapper.AdminLogMapper;
import com.zt.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 管理员操作日志服务实现类
 * @author 周明震
 * @date 2025/7/21
 */
@Service
public class AdminLogServiceImpl extends ServiceImpl<AdminLogMapper, AdminLog> implements AdminLogService {
    
    @Autowired
    private AdminLogMapper adminLogMapper;
    
    @Override
    public void saveLog(AdminLog adminLog) {
        if (adminLog.getCreatedAt() == null) {
            adminLog.setCreatedAt(LocalDateTime.now());
        }
        this.save(adminLog);
    }
    
    @Override
    public void saveLog(Integer adminId, String adminUsername, String module, String operation, 
                       String description, String method, String url, String params, 
                       String result, Integer status, String errorMsg, String ip, 
                       String userAgent, Long executeTime) {
        AdminLog adminLog = new AdminLog();
        adminLog.setAdminId(adminId);
        adminLog.setAdminUsername(adminUsername);
        adminLog.setModule(module);
        adminLog.setOperation(operation);
        adminLog.setDescription(description);
        adminLog.setMethod(method);
        adminLog.setUrl(url);
        adminLog.setParams(params);
        adminLog.setResult(result);
        adminLog.setStatus(status);
        adminLog.setErrorMsg(errorMsg);
        adminLog.setIp(ip);
        adminLog.setUserAgent(userAgent);
        adminLog.setExecuteTime(executeTime);
        adminLog.setCreatedAt(LocalDateTime.now());
        
        this.save(adminLog);
    }
    
    @Override
    public IPage<AdminLog> getLogPage(int pageNum, int pageSize, Map<String, Object> params) {
        Page<AdminLog> page = new Page<>(pageNum, pageSize);
        return adminLogMapper.selectLogPage(page, params);
    }
    
    @Override
    public List<AdminLog> getLogList(Map<String, Object> params) {
        return adminLogMapper.selectLogList(params);
    }
    
    @Override
    public Map<String, Object> getLogStats(LocalDateTime startTime, LocalDateTime endTime) {
        return adminLogMapper.selectLogStats(startTime, endTime);
    }
    
    @Override
    public int cleanExpiredLogs(int days) {
        LocalDateTime beforeTime = LocalDateTime.now().minusDays(days);
        return adminLogMapper.deleteExpiredLogs(beforeTime);
    }
    
    @Override
    public List<AdminLog> exportLogs(Map<String, Object> params) {
        return adminLogMapper.selectLogList(params);
    }

    @Override
    public void clearAllLogs() {
        adminLogMapper.clearAllLogs();
    }
}
